package calao.csp;

public class CommandParser 
{
	
	public final static int MAX_ARGUMENTS = 32;
	
	final String command;
	
	int pos = 0;
	
	String name;
	String[] argv;

	public CommandParser(String command)
	{
		this.command = command;
	}
	
	private String nextToken()
	{
		while (pos<command.length() && command.charAt(pos)==' ') pos++;
		if (pos==command.length()) return null;
		int sepPos = command.indexOf(' ', pos);
		String result;
		if (sepPos==-1) {
			result = command.substring(pos);
			pos = command.length();
		}
		else {
			result = command.substring(pos, sepPos);
			pos = sepPos;
		}
		return result;
	}
	
	public void parse()
	{
		name = nextToken();
		argv = new String[MAX_ARGUMENTS];
		String token;
		int count = 0;
		while ((token=nextToken())!=null) {
			argv[count++] = token;
		}
		argv = java.util.Arrays.copyOf(argv, count);		
	}
	
	public String getCommandName()
	{
		return name;
	}
	
	public String[] getCommandArguments()
	{
		return argv;
	}

}
